package com.gjj.abox.account.viewHolder;

/**
 * Created by DWCloud on 2016/3/31.
 */

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

//import com.facebook.drawee.view.SimpleDraweeView;

/**
 * Created by GUJIAJIA on 2016/3/3.
 * Email 965939858@qq.com
 */
public class CommonViewHolder {
    private final SparseArray<View> mViews;
    private View mConvertView;

    private CommonViewHolder(Context context, ViewGroup parent, int layoutId,
                             int position) {
        this.mViews = new SparseArray<View>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
                false);
        mConvertView.setTag(this);
    }

    public static CommonViewHolder get(Context context, View convertView,
                                       ViewGroup parent, int layoutId, int position) {

        if (convertView == null) {
            return new CommonViewHolder(context, parent, layoutId, position);
        }
        return (CommonViewHolder) convertView.getTag();
    }

    public <T extends View> T getView(int viewId) {

        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public CommonViewHolder setText(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }
    public CommonViewHolder setBackgroud(int viewId,int url){
        ImageView view = getView(viewId);
        view.setBackgroundResource(url);
        return this;
    }

//    public CommonViewHolder setSimpleDraweeView(int viewId,String url){
//        SimpleDraweeView simpleDraweeView = getView(viewId);
//        simpleDraweeView.setImageURI(Uri.parse(url));
//        return this;
//    }

    public View getConvertView() {
        return mConvertView;
    }
}
